<?php

/*   (C) 2010 Pall Thayer and SUNY Purchase College, Purchase, NY
   @license http://www.gnu.org/licenses/gpl.html GNU Public License
*/

require_js(array('yui_yahoo', 'yui_dom', 'yui_event', 'yui_connection', 'yui_json'));

class block_moodlelibreserve extends block_base {

    function init() {
		$this->title = "Library Reserves";
		$this->version = 2010061701;
	}

	function has_config() {
		return true;
	}

	function get_content() {
		global $CFG, $USER, $COURSE;
		$context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
		if ($this->content !== NULL) {
			return $this->content;
		}
		$js_txt = '<script type="text/javascript" src="'.$CFG->wwwroot.'/blocks/moodlelibreserve/libres.js"></script>';

		if($rs = get_records('libres', 'course', $COURSE->id)){
			$pend_req = '<div>Pending Requests:<ul>';
			$proc_req = '<div>Processing Requests:<ul>';
			$publ_req = '<div>Available resources:<ul>';

			foreach($rs as $op){
				if($op->processing == 2){
					$publ_req .= '<li style="font-weight:bold;"><a onmousedown="show_hide(\'libres_call_'.$op->id.'\');return false;">'.$op->title.'</a><div id="libres_call_'.$op->id.'" style="display:none;margin-bottom:3px;" class="libres_tog">Call No.: '.$op->call_num.'</div></li>';
				}elseif($op->processing == 1){
					$proc_req .= '<li style="font-weight:bold;"><a href="">'.$op->title.'</a></li>';
				}else{
					$pend_req .= '<li style="font-weight:bold;"><a href="'.$CFG->wwwroot.'/blocks/'.$this->name().'/instructor.php?cid='.$COURSE->id.'">'.$op->title.'</a></li>';
				}
			}
			$pend_req .= '</ul></div>';
			$publ_req .= '</ul></div>';
			$proc_req .= '</ul></div>';
		}

        $init_text = '<div id="libres-page">';
		$dashboard = '';
		if(has_capability('block/moodlelibreserve:allrequests', $context)){
			$dashboard = '/librarian.php';
		}else{
			$dashboard = '/instructor.php?cid='.$COURSE->id;
		}
		
		if(has_capability('block/moodlelibreserve:makerequest', $context)){
            $init_text .= '<div id="req_page" style="text-align:center;border-top:1px dotted #ccc;padding:3px;"><a href="'.$CFG->wwwroot.'/blocks/'.$this->name().$dashboard.'">';
            $init_text .= "Library Reserve Dashboard";
            $init_text .= "</a></div>";
        }
			$this->content = new stdClass;
			$init_text .= '</div>';
			$this->content->text = $js_txt;
			$this->content->text .= $publ_req;
			if(has_capability('block/moodlelibreserve:makerequest', $context)){
				$this->content->text .= $proc_req;
				$this->content->text .= $pend_req;
				$this->content->text .= $init_text;
			}
			$this->content->footer = '';
		return $this->content;
	}
}
?>
